package org.jackysoft.entity;

import java.util.Collection;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Type;
import org.jackysoft.entity.AbstractEntity;
import org.jackysoft.report.ItemUnit;
import org.jackysoft.report.ProductValidation;
import org.jackysoft.report.visitor.ArmItemVisitor;
import org.joda.time.DateTime;

import com.google.common.collect.Lists;

@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
public class MonthPlanningItem extends AbstractEntity implements Comparable<MonthPlanningItem>,ItemUnit{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4221603899640593095L;
			
	private Double value = 0D;	
	
	private Double modifiedValue = 0D;
	
	private ProductValidation validator;
	
	private String remark;		
	
	private Product owner;
	
	private Product host;
	
	private DateTime executeDate;
	
	private Technics technics;
	
	private MonthPlanItemType itemType = MonthPlanItemType.Proterozoic;
	
	private Collection<PlanItemModifiedRecord> modifies = Lists.newArrayList();

	private long itemStamp;
	
	private Double completed;
	
	public void setValue(Double value) {
		this.value = value;
		
	}

	
	/**
	 * 基础数量，第一次下计划时的量
	 * */
	public Double getValue() {
		return value;
	}

	
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

		

	public void setValidator(ProductValidation validator) {
		this.validator = validator;
	}

	public ProductValidation getValidator() {
		return validator==null?ProductValidation.无校验:validator;
	}
	

	/**
	 * <dd>该计划项所在产品：意即如该计划属半成品或者零件，
	 * <dd>此属性代表此半成品所在的成品
	 * */
	@ManyToOne
	public Product getOwner() {
		return owner;
	}

	public void setOwner(Product ownerProduct) {
		this.owner = ownerProduct;
	}

	

	/**
	 * 最终量,经过调整修改后的计划量
	 * */
	@Transient
	public Double getFinalValue() {
		
		Double tvalue  = 0D;
		for(PlanItemModifiedRecord pr :this.getModifies()){
			tvalue+=pr.getValue();
		}
		
		return tvalue+this.value;
	}

	
	@Transient
	public Collection<PlanItemModifiedRecord> getModifies() {
		return modifies;
	}


	public void setModifies(Collection<PlanItemModifiedRecord> modifies) {
		this.modifies = modifies;
	}

	@Type(type = "jodatime")
	public DateTime getExecuteDate() {
		return executeDate;
	}


	public void setExecuteDate(DateTime executeDate) {
		this.executeDate = executeDate;
	}


	/**
	 * 修正量,可正可负
	 * */
	public Double getModifiedValue() {
		return modifiedValue;
	}


	public void setModifiedValue(Double modifiedValue) {
		this.modifiedValue = modifiedValue;
	}


	public MonthPlanItemType getItemType() {
		return itemType;
	}


	public void setItemType(MonthPlanItemType itemType) {
		this.itemType = itemType;
	}
	
	@Override
	public String toString(){
		return this.host+"-"+this.owner;
	}


	/**
	 * 该计划项所代表的产品
	 * */
	@ManyToOne
	public Product getHost() {
		return host;
	}


	public void setHost(Product hostProduct) {
		this.host = hostProduct;
	}


	@ManyToOne
	public Technics getTechnics() {
		return technics;
	}


	public void setTechnics(Technics technics) {
		this.technics = technics;
	}

	
	public long getItemStamp() {
		return itemStamp;
	}


	public void setItemStamp(long itemStamp) {
		this.itemStamp = itemStamp;
	}


	@Override
	public MonthPlanningItem clone()  {
		return (MonthPlanningItem) super.clone();
	}


	
	@Override
	public void accetp(ArmItemVisitor vistor) {
		vistor.visite(this);		
	}


	@Override
	@Transient
	public Map<String, DataItem> getDataItems() {
		
		return null;
	}


	/**
	 * 当月完成数
	 * */	
	public Double getCompleted() {
		return completed;
	}


	public void setCompleted(Double completed) {
		this.completed = completed;
	}
	
	@Override
	public int compareTo(MonthPlanningItem o) {
		if(o==null) return -1;
		String codea = StringUtils.trimToEmpty(this.getOwner().getCode());
		String codeb = StringUtils.trimToEmpty(o.getOwner().getCode());
		return codea.compareToIgnoreCase(codeb);
	}
	
	
}